package com.home766.service.impl;

import com.home766.dao.IDictionaryDao;
import com.home766.dto.DictionaryDTO;
import com.home766.entity.Dictionary;
import com.home766.service.IDictionaryService;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.service.impl.BaseServiceImpl;
import net.biz.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DictionaryServiceImpl extends BaseServiceImpl<Dictionary, Long> implements IDictionaryService{

	@Autowired
	private IDictionaryDao dictionaryDaoImpl;

	@Autowired
	public void setBaseDao(IDictionaryDao dictionaryDaoImpl) {
		super.setBaseDao(dictionaryDaoImpl);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Dictionary> searchRoots() {
		// TODO Auto-generated method stub
		return dictionaryDaoImpl.findRoots(null);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Dictionary> searchRoots(Integer count) {
		// TODO Auto-generated method stub
		return dictionaryDaoImpl.findRoots(count);
	}

	@Override
	@Transactional(readOnly=true)
	public List<DictionaryDTO> searchChildren(Long dictionaryId) {
		// TODO Auto-generated method stub
		return dictionaryDaoImpl.findChildren(dictionaryId);
	}

	@Override
	@Transactional(readOnly=true)
	public Page<Dictionary> searchPage(Map<String, Object> params, Pageable pageable) {
		// TODO Auto-generated method stub
		return dictionaryDaoImpl.findPage(params,pageable);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Dictionary> searchRoots(Boolean bool, Integer count) {
		// TODO Auto-generated method stub
		return dictionaryDaoImpl.findRoots(bool, count);
	}

	@Override
	@Transactional(readOnly=true)
	public List<DictionaryDTO> searchChildren(Long dictionaryId, boolean bool) {
		// TODO Auto-generated method stub
		return dictionaryDaoImpl.findChildren(dictionaryId, bool);
	}

	@Override
	@Transactional(readOnly=true)
	public String searchAll(Long id) {
		// TODO Auto-generated method stub
		String str="";
		List<Dictionary> list= dictionaryDaoImpl.findAll();
		List<Map<String,Object>> newList= setTreeNode(list,id,false);
		if(newList!=null){
			str= JsonUtils.toJson(newList);
		}
		return str;
	}
	
	private List<Map<String, Object>> setTreeNode(List<Dictionary> dictionarys, Long id, Boolean bool){
		List<Map<String,Object>> list=null;
		if(list==null){
			list=new ArrayList<Map<String,Object>>();
		}
		if(!bool){
			Map<String, Object> root=new HashMap<String,Object>();
			root.put("id", "0");
			root.put("name", "请选择");
			root.put("pId", "0");
			list.add(root);
			bool=true;
		}
		if(null!=dictionarys&&!dictionarys.isEmpty()){
			for(Dictionary dictionary:dictionarys){
				Map<String, Object> map=new HashMap<String,Object>();
				map.put("id", dictionary.getId());
				map.put("name", dictionary.getTitle());
				if(null!=dictionary.getParent()){
					map.put("pId", dictionary.getParent().getId());
				}else{
					map.put("pId", 0);
				}
				if(null!=id){
					if(id.longValue()==dictionary.getId().longValue()){
						map.put("open", true);
						map.put("checked", true);
					}
				}
				list.add(map);
				if(null!=dictionary.getChildrens()&&!dictionary.getChildrens().isEmpty()){
					setTreeNode(dictionary.getChildrens(),id,bool);
				}
			}
		}
		return list;
	}
//	private void setTreeChildNode(List<Dictionary> dictionarys,Long id,Boolean bool,List<Map<String,Object>> list){
//		if(null!=dictionarys&&!dictionarys.isEmpty()){
//			for(Dictionary dictionary:dictionarys){
//				Map<String, Object> map=new HashMap<String,Object>();
//				map.put("id", dictionary.getId());
//				map.put("name", dictionary.getTitle());
//				if(null!=dictionary.getParent()){
//					map.put("pId", dictionary.getParent().getId());
//				}else{
//					map.put("pId", 0);
//				}
//				if(null!=id){
//					if(id.longValue()==dictionary.getId().longValue()){
//						map.put("open", true);
//						map.put("checked", true);
//					}
//				}
//				list.add(map);
//			}
//		}
//	}
	
}
